@echo off
chcp 65001
setlocal

:: --- Configuration ---
set "NODEJS_VERSION=22.17.1"
set "NODEJS_ARCH=x64"
set "NODEJS_DOWNLOAD_URL=https://nodejs.org/dist/v%NODEJS_VERSION%/node-v%NODEJS_VERSION%-%NODEJS_ARCH%.msi"
set "NODEJS_INSTALLER_NAME=node-v%NODEJS_VERSION%-%NODEJS_ARCH%.msi"
set "DOWNLOAD_DIR=%TEMP%\NodejsInstaller"

set "NPM_REGISTRY=https://registry.npmmirror.com"
set "NPM_PACKAGE=@zytech/zycode"

:: --- Script Start ---

echo.
echo ====================================================
echo      Node.js and npm Automatic Installation Script
echo ====================================================
echo.

:: Check for Administrator privileges
net session >nul 2>&1
if %errorLevel% neq 0 (
    echo ERROR: This script must be run as Administrator.
    echo Please right-click the script and select "Run as administrator" .
    pause
    exit /b 1
)

:: --- Step 1: Check if Node.js/npm is installed ---
echo.
echo Checking if Node.js and npm are installed...
where node >nul 2>&1
if %errorlevel% equ 0 (
    echo Node.js is already installed. Skipping to npm configuration.
    goto :CONFIGURE_NPM
)

echo Node.js is not installed. Starting download and installation process.

:: --- Step 2: Install Node.js ---
echo.
echo --- Starting Node.js Installation ---

:: Create download directory
if not exist "%DOWNLOAD_DIR%" (
    echo Creating download directory: %DOWNLOAD_DIR%
    mkdir "%DOWNLOAD_DIR%"
)

:: Download Node.js installer using PowerShell
echo.
echo Downloading Node.js installer (%NODEJS_INSTALLER_NAME%)...
echo From: %NODEJS_DOWNLOAD_URL%
powershell -Command "Invoke-WebRequest -Uri '%NODEJS_DOWNLOAD_URL%' -OutFile '%DOWNLOAD_DIR%\%NODEJS_INSTALLER_NAME%'"
if %errorlevel% neq 0 (
    echo ERROR: Failed to download Node.js installer. Check your network or the URL.
    pause
    exit /b 1
)
echo Download complete.

:: Silently install Node.js
echo.
echo Installing Node.js silently...
echo This may take a few moments. Please wait.
msiexec /i "%DOWNLOAD_DIR%\%NODEJS_INSTALLER_NAME%" /qn /norestart
if %errorlevel% neq 0 (
    echo ERROR: Node.js installation failed. Error code: %errorlevel%
    pause
    exit /b 1
)

:: Clean up the installer
echo.
echo Cleaning up downloaded files...
del /q "%DOWNLOAD_DIR%\%NODEJS_INSTALLER_NAME%"
rmdir "%DOWNLOAD_DIR%"
echo Cleanup complete.

echo.
echo =======================================================================
echo  Node.js has been installed successfully!
echo.
echo  IMPORTANT:
echo  To activate the 'node' and 'npm' commands, you must restart this script.
echo  Please close this window and run this script AGAIN as an Administrator.
echo =======================================================================
echo.
pause
exit /b 0


:CONFIGURE_NPM
:: --- Step 3: Configure npm to use China mirror ---
echo.
echo Configuring npm to use registry: %NPM_REGISTRY%
npm config set registry %NPM_REGISTRY%
if %errorlevel% neq 0 (
    echo ERROR: Failed to configure npm registry.
    pause
    exit /b 1
)
echo npm registry configured successfully.

:: Verify the new registry
echo Current npm registry:
npm config get registry
echo.

:: --- Step 4: Install the specified global npm package ---
echo.
echo Installing global npm package: %NPM_PACKAGE%
npm install -g %NPM_PACKAGE%
if %errorlevel% neq 0 (
    echo ERROR: Failed to install npm package %NPM_PACKAGE%.
    echo Please check your network or the package name.
    pause
    exit /b 1
)
echo Package %NPM_PACKAGE% installed successfully.

:END
echo.
echo ====================================================
echo           Script execution finished!
echo ====================================================
echo.
pause
endlocal
